package com.seu.vcampus.view;

import com.seu.vcampus.vo.User;

import javax.swing.*;
import java.awt.*;

/**
 * 用户编辑对话框
 * 
 * @author 虚拟校园开发团队
 * @since 1.0.0
 */
public class UserDialog extends JDialog {
    private User user;
    private boolean confirmed = false;
    
    private JTextField usernameField;
    private JPasswordField passwordField;
    private JTextField emailField;
    private JTextField phoneField;
    private JComboBox<String> userTypeComboBox;
    private JCheckBox activeCheckBox;
    
    public UserDialog(Frame parent, User user) {
        super(parent, user == null ? "添加用户" : "编辑用户", true);
        this.user = user;
        initializeComponents();
        setupLayout();
        setupEventHandlers();
        loadUserData();
    }
    
    /**
     * 初始化组件
     */
    private void initializeComponents() {
        setSize(500, 400); // 增大对话框尺寸
        setLocationRelativeTo(getParent());
        setResizable(false);
        
        usernameField = new JTextField(20);
        passwordField = new JPasswordField(20);
        emailField = new JTextField(20);
        phoneField = new JTextField(20);
        
        String[] userTypes = {"学生", "教师", "管理员", "图书馆管理员", "系统管理员", "财务管理员"};
        userTypeComboBox = new JComboBox<>(userTypes);
        
        activeCheckBox = new JCheckBox("启用账户");
        activeCheckBox.setSelected(true);
        
        // 设置字体
        Font font = new Font("微软雅黑", Font.PLAIN, 14);
        usernameField.setFont(font);
        passwordField.setFont(font);
        emailField.setFont(font);
        phoneField.setFont(font);
        userTypeComboBox.setFont(font);
        activeCheckBox.setFont(font);
    }
    
    /**
     * 设置布局
     */
    private void setupLayout() {
        JPanel mainPanel = new JPanel(new GridBagLayout());
        mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        
        // 用户名
        gbc.gridx = 0;
        gbc.gridy = 0;
        JLabel usernameLabel = new JLabel("用户名:");
        usernameLabel.setForeground(Color.BLACK);
        mainPanel.add(usernameLabel, gbc);
        
        gbc.gridx = 1;
        mainPanel.add(usernameField, gbc);
        
        // 密码
        gbc.gridx = 0;
        gbc.gridy = 1;
        JLabel passwordLabel = new JLabel("密码:");
        passwordLabel.setForeground(Color.BLACK);
        mainPanel.add(passwordLabel, gbc);
        
        gbc.gridx = 1;
        mainPanel.add(passwordField, gbc);
        
        // 邮箱
        gbc.gridx = 0;
        gbc.gridy = 2;
        JLabel emailLabel = new JLabel("邮箱:");
        emailLabel.setForeground(Color.BLACK);
        mainPanel.add(emailLabel, gbc);
        
        gbc.gridx = 1;
        mainPanel.add(emailField, gbc);
        
        // 电话
        gbc.gridx = 0;
        gbc.gridy = 3;
        JLabel phoneLabel = new JLabel("电话:");
        phoneLabel.setForeground(Color.BLACK);
        mainPanel.add(phoneLabel, gbc);
        
        gbc.gridx = 1;
        mainPanel.add(phoneField, gbc);
        
        // 用户类型
        gbc.gridx = 0;
        gbc.gridy = 4;
        JLabel userTypeLabel = new JLabel("用户类型:");
        userTypeLabel.setForeground(Color.BLACK);
        mainPanel.add(userTypeLabel, gbc);
        
        gbc.gridx = 1;
        mainPanel.add(userTypeComboBox, gbc);
        
        // 启用状态
        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.gridwidth = 2;
        mainPanel.add(activeCheckBox, gbc);
        
        // 按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 0));
        JButton okButton = new JButton("确定");
        JButton cancelButton = new JButton("取消");
        
        buttonPanel.add(okButton);
        buttonPanel.add(cancelButton);
        
        gbc.gridy = 6;
        mainPanel.add(buttonPanel, gbc);
        
        add(mainPanel);
        
        // 设置按钮事件
        okButton.addActionListener(e -> {
            if (validateInput()) {
                saveUser();
                confirmed = true;
                dispose();
            }
        });
        
        cancelButton.addActionListener(e -> dispose());
    }
    
    /**
     * 设置事件处理器
     */
    private void setupEventHandlers() {
        // 已在setupLayout中设置
    }
    
    /**
     * 加载用户数据
     */
    private void loadUserData() {
        if (user != null) {
            usernameField.setText(user.getUsername());
            usernameField.setEnabled(false); // 编辑时不允许修改用户名
            emailField.setText(user.getEmail() != null ? user.getEmail() : "");
            phoneField.setText(user.getPhone() != null ? user.getPhone() : "");
            
            // 设置用户类型
        switch (user.getUserType()) {
            case STUDENT:
                userTypeComboBox.setSelectedIndex(0);
                break;
            case TEACHER:
                userTypeComboBox.setSelectedIndex(1);
                break;
            case ADMIN:
                userTypeComboBox.setSelectedIndex(2);
                break;
            case LIBRARY_ADMIN:
                userTypeComboBox.setSelectedIndex(3);
                break;
            default:
                userTypeComboBox.setSelectedIndex(0);
                break;
        }
            
            activeCheckBox.setSelected(user.isActive());
        }
    }
    
    /**
     * 验证输入
     */
    private boolean validateInput() {
        String username = usernameField.getText().trim();
        String password = new String(passwordField.getPassword()).trim();
        
        if (username.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请输入用户名", "提示", JOptionPane.WARNING_MESSAGE);
            return false;
        }
        
        if (user == null && password.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请输入密码", "提示", JOptionPane.WARNING_MESSAGE);
            return false;
        }
        
        return true;
    }
    
    /**
     * 保存用户
     */
    private void saveUser() {
        if (user == null) {
            user = new User();
        }
        
        user.setUsername(usernameField.getText().trim());
        
        String password = new String(passwordField.getPassword()).trim();
        if (!password.isEmpty()) {
            user.setPassword(password);
        }
        
        user.setEmail(emailField.getText().trim());
        user.setPhone(phoneField.getText().trim());
        
        // 设置用户类型
        String selectedType = (String) userTypeComboBox.getSelectedItem();
        switch (selectedType) {
            case "学生":
                user.setUserType(User.UserType.STUDENT);
                break;
            case "教师":
                user.setUserType(User.UserType.TEACHER);
                break;
            case "管理员":
                user.setUserType(User.UserType.ADMIN);
                break;
            case "图书馆管理员":
                user.setUserType(User.UserType.LIBRARY_ADMIN);
                break;
            case "系统管理员":
                user.setUserType(User.UserType.SYSTEM_ADMIN);
                break;
            case "财务管理员":
                user.setUserType(User.UserType.FINANCE_ADMIN);
                break;
            default:
                user.setUserType(User.UserType.STUDENT);
                break;
        }
        
        user.setActive(activeCheckBox.isSelected());
    }
    
    /**
     * 获取用户对象
     */
    public User getUser() {
        return user;
    }
    
    /**
     * 是否已确认
     */
    public boolean isConfirmed() {
        return confirmed;
    }
}